package com.dxf.sysadmin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxf.common.utils.ResultJson;
import com.dxf.sysadmin.entity.RoleFunc;
import com.dxf.sysadmin.entity.RoleMenu;
import com.dxf.sysadmin.entity.SysRole;
import com.dxf.sysadmin.entity.po.SysRolePo;
import com.dxf.sysadmin.entity.qo.SysRoleQo;
import com.dxf.sysadmin.mapper.SysRoleMapper;
import com.dxf.sysadmin.service.*;
import com.dxf.sysadmin.utils.LayuiTree;
import com.dxf.sysadmin.utils.TreeHpler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 角色表 服务实现类
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysFuncService sysFuncService;
    @Autowired
    private RoleFuncService roleFuncService;
    @Autowired
    private RoleMenuService roleMenuService;
    /**
     *角色列表分页查询
     */
    @Override
    public ResultJson rolePage(SysRoleQo qry) {
        Page<SysRole> page = new Page<>(qry.getPage(), qry.getLimit());
        IPage<SysRole> role = page(page);

        Map<String, Object> map = new HashMap<>();
        map.put("rows",role.getRecords());
        map.put("total",role.getTotal());
        return ResultJson.ok().data(map);
    }

    /**
     *根据id获取角色详情
     */
    @Override
    public ResultJson details(Long rid) {
        SysRolePo sysRolePo = new SysRolePo();
        List<LayuiTree> roleTree = TreeHpler.getRoleTree(sysMenuService, sysFuncService);
        List<String> ids = TreeHpler.getIds(rid, roleMenuService, roleFuncService);
        SysRole byId = getById(rid);
        BeanUtils.copyProperties(byId,sysRolePo);
        sysRolePo.setTrees(roleTree);
        sysRolePo.setIds(ids);
        return ResultJson.ok().data("sysRolePo",sysRolePo);
    }

    /**
     *根据SysRolePo对象添加或修改角色及权限
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson saveOrUpdate(SysRolePo po) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(po,sysRole);
        saveOrUpdate(sysRole);

        Map<String, List<Long>> webIds = TreeHpler.getWebIds(po.getTrees());

        List<RoleMenu> menuList=new ArrayList<>();
        List<RoleFunc> funcList=new ArrayList<>();
        webIds.get("menuIds").forEach(x->{
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(sysRole.getId());
            roleMenu.setMenuId(x.longValue());
            //不包含才添加
            if (!menuList.contains(roleMenu)){
                menuList.add(roleMenu);
            }
        });
        webIds.get("funcIds").forEach(y->{
            RoleFunc roleFunc = new RoleFunc();
            roleFunc.setRoleId(sysRole.getId());
            roleFunc.setFuncId(y.longValue());
            //不包含才添加
            if (!funcList.contains(roleFunc)){
                funcList.add(roleFunc);
            }
        });
        if (po.getId()==null){
            //新增
            roleFuncService.saveBatch(funcList);
            roleMenuService.saveBatch(menuList);
        }else {
            //修改
            roleFuncService.removeById(po.getId());
            roleFuncService.saveBatch(funcList);

            roleMenuService.removeById(po.getId());
            roleMenuService.saveBatch(menuList);
        }
        return ResultJson.ok();
    }

    /**
     *更具id列表删除角色
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson deleteUser(List<Long> idList) {
        removeByIds(idList);
        roleFuncService.removeByIds(idList);
        roleMenuService.removeByIds(idList);
        return ResultJson.ok();
    }

    /**
     *获取角色权限树数据
     */
    @Override
    public ResultJson getRoleTree() {
        List<LayuiTree> roleTree = TreeHpler.getRoleTree(sysMenuService, sysFuncService);
        return ResultJson.ok().data("roleTree",roleTree);
    }

}
